﻿namespace Pibby.Core.Opml
{
    using System;
    using System.Xml;
    
	/// <summary>
	/// A single entry in an OPML document
	/// </summary>
	public sealed class Outline
	{
		#region Xml Constants

		public const string OutlineNode = "outline";
		public const string TextAttribute = "text";
		public const string TitleAttribute = "title";
		public const string TypeAttribute = "type";
		public const string RssType = "rss";
		public const string XmlAttribute = "xmlUrl";
		public const string HtmlAttribute = "htmlUrl";

		#endregion

		#region Constructors

		public Outline()
		{
		}

		/// <summary>
		/// Create a new outline object from an xml fragment
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", MessageId = "System.Xml.XmlNode")]
		public static Outline Create(XmlElement element)
		{
			if (element == null)
				throw new ArgumentNullException("element", Resources.Outline_NullObjectError);

			var enclosure = new Outline();

			if (element.Name.Equals(OutlineNode, StringComparison.CurrentCultureIgnoreCase))
			{
				if (element.Attributes[TextAttribute] != null)
					enclosure.Text = element.Attributes[TextAttribute].Value;

				if (element.Attributes[TitleAttribute] != null)
					enclosure.Title = element.Attributes[TitleAttribute].Value;

				if (element.Attributes[TypeAttribute] != null)
				{
					if (element.Attributes[TypeAttribute].Value.Equals(Outline.RssType, StringComparison.CurrentCultureIgnoreCase))
						enclosure.OutlineType = OutlineType.Rss;
					else
						enclosure.OutlineType = OutlineType.Unknown;
				}

				if (element.Attributes[XmlAttribute] != null)
					enclosure.XmlUrl = new Uri (element.Attributes[XmlAttribute].Value);

				if (element.Attributes[HtmlAttribute] != null)
					enclosure.HtmlUrl = new Uri (element.Attributes[HtmlAttribute].Value);
			}

			return enclosure;
		}

		#endregion
		
        #region Properties

		public string Text
		{
			get;
			set;
		}

		public string Title
		{
			get;
			set;
		}

		public OutlineType OutlineType
		{
			get;
			set;
		}

		public Uri XmlUrl
        {
			get;
			set;
        }

		public Uri HtmlUrl
		{
			get;
			set;
		}

        #endregion
	}
}
